(function(angular) {
	'use strict';
	angular.module('fs.waf').controller('apf.piCreate.createBuyerVerifyController', createBuyerVerifyController);
	createBuyerVerifyController.$inject = [ '$scope', 'Constants', 'modalService', 'gridService', 'layoutConfig', 'validatorConfig', 'authService',
			'piCreateService'];
	function createBuyerVerifyController($scope, Constants, modalService, gridService, layoutConfig, validatorConfig, authService, piCreateService) {
		var ctrl = this, piInfo = $scope.$stateParams.params, errorMessage = {};
		ctrl.layoutConfig = layoutConfig;
		ctrl.validatorConfig = validatorConfig;
		ctrl.piLoad = {};
		ctrl.piLoad.pi = {};
		
		ctrl.init = function() {
			ctrl.invoiceTypeChanged = function(rowObj, form) {
				if (rowObj.invoiceType) {
					// reset paycodeOptions.data
					if (rowObj._refTypeConfig && rowObj._refTypeConfig.options){
						angular.forEach(ctrl.invoiceTypeOptions, function(invoiceType, index) {
							if(rowObj.invoiceType == invoiceType.value){
								rowObj._refTypeConfig.options = invoiceType.extProperty.refTypeOptions;
								rowObj.isAdjAmtShow = invoiceType.extProperty.isAdjAmtShow;
								rowObj.isAdjCodeShow = invoiceType.extProperty.isAdjCodeShow;
								rowObj.isDiscAmtShow = invoiceType.extProperty.isDiscAmtShow;
								rowObj.isGrossAmtShow = invoiceType.extProperty.isGrossAmtShow;
							}
						});
					}
				}
			};
			piCreateService.getCalendarCodes().then(function(response) {
				ctrl.calendarCodes = response.data;
			});
			piCreateService.getCurrencyOptions().then(function(response) {
				ctrl.currencyOptions = response.data;
			});
			piCreateService.getCommunityCodeQOptions().then(function(response){
				ctrl.communityCodeQOptions = response.data;
			});
			authService.createBuyerVerify(piInfo).then(function(response) {
				
				if (response.data && response.data.model && response.data.task) {
					var piLoad = response.data.model;
					piLoad.partyType = "BUYER";
					var task = response.data.task;
					
					ctrl.innerGridOptions = gridService.createInnerGridOptions($scope, ctrl.layoutConfig.formConfigs.invoicesCfg.tableConfigs.table, rowSelectionChanged);
					ctrl.innerGridOptions.data = 'vm.piLoad.pi.invoiceList';
					ctrl.innerGridOptions.enableRowHeaderSelection = false;
					
					piCreateService.invoiceTypeOptions(piLoad).then(function(response){
						ctrl.invoiceTypeOptions = response.data;
						ctrl.piLoad = piLoad;
						ctrl.piLoad.pi.invoiceList = ctrl.piLoad.pi.invoiceList || [];
						ctrl.piLoad.pi.invoiceDeleteList = ctrl.piLoad.pi.invoiceDeleteList || [];
						ctrl.piLoad.taskId = task.taskId;
						ctrl.resetInvoiceListByData();
					});
				};
			});
		};
		
		ctrl.resetInvoiceListByData = function(isView) {
			angular.forEach(ctrl.piLoad.pi.invoiceList, function(invoice, index) {
				invoice._refTypeConfig = {
						model : "refType",
						type : "select",
						options : {},
						readonly : true
				};
				if (isView) invoice._refTypeConfig.readonly = true;
			});
		};
		
		ctrl.createBuyerVerifyReject = function() {
			modalService.openPromptDialogs({
				'btnCallback' : {
					'ok' : function(comments) {
						ctrl.piLoad.message = comments;
						authService.createBuyerVerifyReject(ctrl.piLoad).then(rejectSuccessful, rejectFailed);
					}
				}
			});
		};

		ctrl.createBuyerVerifyApprove = function() {
			modalService.openConfirmDialogs({
				'messages' : [ {msg : 'title.dialog.confirm.approve', args : [ctrl.piLoad.primaryKey]} ],
				'btnCallback' : {
					'ok' : function() {
						authService.createBuyerVerifyApprove(ctrl.piLoad).then(approveSuccessful, approveFailed);
					}
				}
			});
		};

		function approveSuccessful(response) {
			modalService.openAcknowledgeDialogs({
				'messages' : [ 'title.dialog.successful.approve' ],
				'btnCallback' : {
					'close' : function() {
						$scope.go('apf.workflow.wfTxn.list');
					}
				},
				'redirections' : [ {
					'name' : 'title.infobar.workflow.list',
					'state' : 'apf.workflow.wfTxn.list',
					'params' : {}
				}, {
					'name' : 'title.infobar.pi.create',
					'state' : 'apf.piCreate.create',
					'params' : {}
				}]
			});
		}
		
		
		function approveFailed(response) {
			modalService.openErrorDialogs({
				'messages' : response.error
			});
		}

		function rejectSuccessful(response) {
			modalService.openAcknowledgeDialogs({
				'messages' : [ 'title.dialog.successful.reject' ],
				'btnCallback' : {
					'close' : function() {
						$scope.go('apf.workflow.wfTxn.list');
					}
				},
				'redirections' : [ {
					'name' : 'title.infobar.workflow.list',
					'state' : 'apf.workflow.wfTxn.list',
					'params' : {}
				}, {
					'name' : 'title.infobar.pi.create',
					'state' : 'apf.piCreate.create',
					'params' : {}
				}]
			});
		}

		function rejectFailed(response) {
			modalService.openErrorDialogs({
				'messages' : response.error
			});
		}
		
		function rowSelectionChanged(eventRow, selectedRows) {
			ctrl.piLoad.selectedInvoices = selectedRows;
		}
		
	}
})(angular);